var QQMapWX = require('../../utils/qqmap-wx-jssdk.min');
var qqmapsdk = new QQMapWX({
  key: "JZUBZ-UOGRS-GLLON-6Y5OW-XBZUJ-KPBBN"
});

Page({
  data: {
    polyline: [],
    longitude: 113.384272,
    latitude: 23.137942,
    markers: [{
      iconPath: "/images/location.png",
      id: 0,
      latitode: 23.099994,
      longitude: 113.324520,
      width: 50,
      height: 50
    }]
  },
  onReady() {
    var _this = this;
    //调用距离计算接口
    qqmapsdk.direction({
      mode: 'driving', //可选值：'driving'（驾车）、'walking'（步行）、'bicycling'（骑行），不填默认：'driving',可不填
      //from参数不填默认当前地址
      from: {
        longitude: 113.3245211,
        latitude: 23.10229
      },
      to: {
        longitude: 113.324520,
        latitude: 23.21229
      },
      success: function (res) {
        console.log(res);
        var ret = res;
        var coors = ret.result.routes[0].polyline,
          pl = [];
        //坐标解压（返回的点串坐标，通过前向差分进行压缩）
        var kr = 1000000;
        for (var i = 2; i < coors.length; i++) {
          coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
        }
        //将解压后的坐标放入点串数组pl中
        for (var i = 0; i < coors.length; i += 2) {
          pl.push({
            latitude: coors[i],
            longitude: coors[i + 1]
          })
        }
        console.log(pl)
        //设置polyline属性，将路线显示出来,将解压坐标第一个数据作为起点
        _this.setData({
          latitude: pl[0].latitude,
          longitude: pl[0].longitude,
          polyline: [{
            points: pl,
            color: '#FF0000DD',
            width: 4
          }]
        })
      },


    });
  }
})